<?php

/********************************************
	[Good8 sns!] (C)2006-2007 Tupa Inc.
	This is NOT a freeware

	$Author: 苏小雨 陶烽
	$Msn: webmaster@good8.com
	$Date: 2007-2-10
********************************************/

function sw_obj($w,$h,$t,$u)
{
	if (strcasecmp($t, "y") == 0)
		$play = "true";
	else
		$play = "false";
	return '<object classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" codebase="http://fpdownload.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=7,0,0,0" width="'.$w.'" height="'.$h.'" align="top"><param name="allowScriptAccess" value="sameDomain"><param name="movie" value="'.$u.'"><param name="quality" value="high"><param name="bgcolor" value="#FFFFFF"><param name="play" value="'.$play.'"><embed src="'.$u.'" width="'.$w.'" height="'.$h.'" play="'.$play.'" align="top" quality="high" bgcolor="#ffffff" allowScriptAccess="sameDomain" type="application/x-shockwave-flash" pluginspage="http://www.macromedia.com/go/getflashplayer"/></object>';
}

function wm_obj($w,$h,$t,$u)
{
	if (strcasecmp($t, "y") == 0)
		$play = "true";
	else
		$play = "false";
	return '<object classid="clsid:6BF52A52-394A-11d3-B153-00C04F79FAA6" width="'.$w.'" height="'.$h.'"><param name="URL" value="'.$u.'"><param name="autoStart" value="'.$play.'"><param name="uiMode" value="mini"><param name="playCount" value="99"><embed url="'.$u.'" width="'.$w.'" height="'.$h.'" autoStart="'.$play.'" uiMode="none" playCount="99"/></object>';
}

function rm_obj($w,$h,$t,$u)
{
	if (strcasecmp($t, "y") == 0)
		$play = "true";
	else
		$play = "false";
	return '<object classid="clsid:CFCDAA03-8BE4-11CF-B84B-0020AFBBCCFA" width="'.$w.'" height="'.$h.'"><param name="AUTOSTART" value="'.$play.'"><param name="CONTROLS" value="ControlPanel"><param name="LOOP" value="0"><param name="BACKGROUNDCOLOR" value="#000000"><param name="SRC" value="'.$u.'"><embed src="'.$u.'" width="'.$w.'" height="'.$h.'" loop="0" controls="ImageWindow" autostart="'.$play.'"/></object>';
}



function qt_obj($w,$h,$t,$u)
{
	if (strcasecmp($t, "y") == 0)
		$play = "true";
	else
		$play = "false";
	return '<object classid="clsid:02BF25D5-8C17-4B23-BC80-D3488ABDDC6B" width="'.$w.'" height="'.$h.'" codebase="http://www.apple.com/qtactivex/qtplugin.cab"><param name="src" value="'.$u.'"><param name="AUTOPLAY" value="'.$play.'"><param name="BGCOLOR" value="#000000"><param name="controller" value="false"><embed pluginspage="www.apple.com/quicktime/download" type="video/quicktime" src="'.$u.'" width="'.$w.'" bgcolor="#000000" height="'.$h.'" autoplay="'.$play.'" controller="false"/></object>';
}

$GCACHE['ubbcodes'] = Array
	(
	'search' => Array
		(
		1 => '/\\[swf=(\d{1,3}),(\d{1,3}):([yn]{1})](.+?)\\[\\/swf\\]/ies',
		2 => '/\\[rm=(\d{1,3}),(\d{1,3}):([yn]{1})](.+?)\\[\\/rm\\]/ies',
		3 => '/\\[wm=(\d{1,3}),(\d{1,3}):([yn]{1})](.+?)\\[\\/wm\\]/ies',
		4 => '/\\[qt=(\d{1,3}),(\d{1,3}):([yn]{1})](.+?)\\[\\/qt\\]/ies',
		),
	'replace' => Array
		(
		
		1 => "sw_obj('\\1','\\2','\\3','\\4')",
		2 => "rm_obj('\\1','\\2','\\3','\\4')",
		3 => "wm_obj('\\1','\\2','\\3','\\4')",
		4 => "qt_obj('\\1','\\2','\\3','\\4')",
		)
	);

function ubb($message,$allowubbcode = 1,$allowmultimedia = 1)//第2个参数，是否允许使用UBB - 第3个参数，是否允许使用笑脸:)暂不使用 - 第4个参数换多少个笑脸
{
	if(!empty($GLOBALS['CONF']['smilies']) && is_array($GLOBALS['CONF']['smilies'])) {
		$message = preg_replace($GLOBALS['CONF']['smilies']['searcharray'], $GLOBALS['CONF']['smilies']['replacearray'], $message, $maxsmiles);
	}
	///以上是笑脸控制，暂时别鸟它

	if(!empty($allowubbcode)) {
		if(empty($goodcodes['search'])) {
			$goodcodes['search']['ubb_regexp'] = array(
				"/\[url\]\s*(www.|https?:\/\/|ftp:\/\/|gopher:\/\/|news:\/\/|telnet:\/\/|rtsp:\/\/|mms:\/\/|callto:\/\/|ed2k:\/\/){1}([^\[\"']+?)\s*\[\/url\]/ie",
				"/\[url=www.([^\[\"']+?)\](.+?)\[\/url\]/is",
				"/\[url=(https?|ftp|gopher|news|telnet|rtsp|mms|callto|ed2k){1}:\/\/([^\[\"']+?)\](.+?)\[\/url\]/is",
				"/\[email\]\s*([a-z0-9\-_.+]+)@([a-z0-9\-_]+[.][a-z0-9\-_.]+)\s*\[\/email\]/i",
				"/\[email=([a-z0-9\-_.+]+)@([a-z0-9\-_]+[.][a-z0-9\-_.]+)\](.+?)\[\/email\]/is",
				"/\[img\]([^\[\"']+?)\[\/img\]/is",
				"/\[img=([^\[\"']+?)\]\[\/img\]/is",
				"/\[color=([^\[\<]+?)\]/i",
				"/\[size=(\d+?)\]/i",
				"/\[size=(\d+(px|pt|in|cm|mm|pc|em|ex|%)+?)\]/i",
				"/\[font=([^\[\<]+?)\]/i",
				"/\[([a-z]+)=(\d+?)\]/ie"
			);
			$goodcodes['replace']['ubb_regexp'] = array(
				"cut_url('\\1\\2')",
				"<a href=\"http://www.\\1\" target=\"_blank\">\\2</a>",
				"<a href=\"\\1://\\2\" target=\"_blank\">\\3</a>",
				"<a href=\"mailto:\\1@\\2\">\\1@\\2</a>",
				"<a href=\"mailto:\\1@\\2\">\\3</a>",
				"<span class=\"psd\"><img name=\"ubbimage\" src=\"\\1\" border=\"0\"/></span>",
				"<span class=\"psd\"><img name=\"ubbimage\" src=\"\\1\" border=\"0\"/></span>",
				"<span style='color:\\1'>",
				"<span style='font-size:\\1px'>",
				"<span style='font-size:\\1;line-height:auto;'>",
				"<span style='font-family:\\1'>",
				"smail('\\1','\\2')",
			);

			$goodcodes['search']['ubb_str'] = array(
				'[/color]', '[/size]', '[/font]', '[b]', '[/b]',
				'[i]', '[/i]', '[u]', '[/u]', '[s]', '[/s]', '[quote]', '[/quote]','[hr]','[br]'
			);

			$goodcodes['replace']['html_str'] = array(
				'</span>', '</span>', '</span>', '<span style=\'font-weight:bold\'>', '</span>', '<span style=\'font-style:italic\'>',
				'</span>', '<span style=\'text-decoration:underline\'>', '</span>', '<span style=\'text-decoration:line-through\'>', '</span>', 
				'<blockquote>', '</blockquote>','<hr />','<br />'
			);
		}
		if ($allowmultimedia == 1)
			$message = str_ireplace($goodcodes['search']['ubb_str'],$goodcodes['replace']['html_str'],preg_replace(array_merge($goodcodes['search']['ubb_regexp'],$GLOBALS['GCACHE']['ubbcodes']['search']),array_merge($goodcodes['replace']['ubb_regexp'],$GLOBALS['GCACHE']['ubbcodes']['replace']),$message));
		else
			$message = str_ireplace($goodcodes['search']['ubb_str'],$goodcodes['replace']['html_str'],preg_replace($goodcodes['search']['ubb_regexp'],$goodcodes['replace']['ubb_regexp'],$message));
		return $message;
	}
}

function cut_url($url) {//截取超级连接2头，中间用省略号表示 比如 www.xxxxsadfs.com/.../sdfsaf.php
	$length = 65;
	$urllink = "<a href=\"".(substr(strtolower($url), 0, 4) == 'www.' ? "http://$url" : $url).'" target="_blank">';
	if(strlen($url) > $length) {
		$url = substr($url, 0, intval($length * 0.5)).' ... '.substr($url, - intval($length * 0.3));
	}
	$urllink .= $url.'</a>';
	return $urllink;
}

function smail($d,$p){
	$dir = "resource/smiles/".$d;
	$file = $dir . "/$p" . ".gif";
	if (file_exists(ROOT_PATH.$file)) {
		return "<img src=\"/".$file."\" border=\"0\"\/>";
	}
	return "[$d:$p]";
}
?>